<?php


namespace app\common\service\order\coupon_order\meituan;


use app\model\AgentServiceConfig;
use app\model\CouponOrder;
use think\Exception;

class MeituanPubUnionSyncOrder extends MeituanChannelBase
{

    //美团状态
    // 状态： https://inews.gtimg.com/newsapp_ls/0/14888831794/0.jiketuchuang.png
    const ITEMBIZ_STATUS_PAY_SUCCESS = 1;
    const ITEMBIZ_STATUS_WRITEOFF_SUCCESS = 2;
    const ITEMBIZ_STATUS_SETTLE_SUCCESS = 3;
    const ITEMBIZ_STATUS_INVALID = 99;
    const ITEMBIZ_STATUS_UNKNOWN = 999;


    public function syncOrder()
    {
        //没有在开发计划中的佣金不结算
        if (empty($this->getServType())) { //可以看下聚推客的
            return false;
        }

        if (empty($this->extend->supplierInfo)){
            throw new Exception('坏了，拉美团分销联盟订单时候，没有胡渠道供应商信息');
        }

        if ($this->extend->supplierInfo->serv_type != $this->serv_type) {
            return '';
        }; //不是该供应商类型的直接过滤了

        /**
         * @var CouponOrder $couponOrder
         */
        $couponOrder = $this->saveOrder();


        //状态变化：
        //1.下单了还没结算，判断转为待结算
        //2.下单了状态为支付成功，但是又退款了，难受


        if ($this->agent_settle_status == CouponOrder::C_AGENT_SETTLE_STATUS_WAIT && $couponOrder->agent_settle_status == CouponOrder::C_AGENT_SETTLE_STATUS_NO && !empty($couponOrder->profit)) {
            $couponOrder->log .= date('Y-m-d H:i:s') . '修改为待结算';
            $couponOrder->order_finish_at = date('Y-m-d H:i:s');
            $couponOrder->agent_settle_status = CouponOrder::C_AGENT_SETTLE_STATUS_WAIT;
            $couponOrder->ext_data = $this->params;
            $couponOrder->save();
        }

//
//        //可能支付时间没有值
//        if (!empty($this->settled_at)  && empty($couponOrder->settled_at)){
//            $couponOrder->settled_at = $this->settled_at;
//            $couponOrder->ext_data = $this->params;
//            $couponOrder->save();
//        }

        //查看进行中和已使用的状态
        if ($this->status != $couponOrder->status) {
            $couponOrder->status = $this->status;
            $couponOrder->refund_status = $this->refund_status;
            $couponOrder->ext_data = $this->params;
            $couponOrder->log .= date('Y-m-d H:i:s') . '修改状态为' . $this->status;
            $couponOrder->save();
        }

        //退款中 https://inews.gtimg.com/newsapp_ls/0/14888931121/0.jiketuchuang.png
        if ($this->refund_status != $couponOrder->refund_status) {
            $couponOrder->refund_status = $this->refund_status;
            $couponOrder->ext_data = $this->params;
            $couponOrder->log .= date('Y-m-d H:i:s') . '修改退款状态为' . $this->refund_status;
            $couponOrder->save();
        }


        if ($this->status == CouponOrder::C_STATUS_RECHARGE_FAIL && $couponOrder->status == CouponOrder::C_STATUS_RECHARGE_SUCCESS) {

            //这个只是万一
            if ($couponOrder->agent_settle_status == CouponOrder::C_AGENT_SETTLE_STATUS_WAIT) {
                $couponOrder->agent_settle_status = CouponOrder::C_AGENT_SETTLE_STATUS_NO;
            }

            $couponOrder->log .= date('Y-m-d H:i:s') . '待结算改为无效订单';
            $couponOrder->refund_status = CouponOrder::C_REFUND_STATUS_HAVE; //已退款筛选
            $couponOrder->status = CouponOrder::C_STATUS_RECHARGE_FAIL;
            $couponOrder->ext_data = $this->params;
            $couponOrder->save();
        }


        return $couponOrder;
    }

    public function getServType()
    {
        $this->processSid();

        if (!empty($this->couponOrderSid)) {
            if (!empty($this->couponOrderSid->serv_type)) {
                return $this->couponOrderSid->serv_type;
            }
        }

        //https://inews.gtimg.com/newsapp_ls/0/14888791002/0.jiketuchuang.png
        $title = $this->getTitle();

        if (strstr($title, '团单')) {
            $serv_type = PRODUCT_SECKILL_SERV;
        } else {
            $serv_type = PRODUCT_METUAN_SERV;
        }
        return $serv_type;

//        if ($this->_income_type != AgentServiceConfig::INCOME_TYPE_SELF && $this->extend->supplierInfo->serv_type != $serv_type){
//            throw new Exception('美团联盟的类型不符合供应商的，稍后再试');
//        }

    }

    public function getProductName()
    {
        return $this->params['shopName'] ?? '';
    }

    public function getAgentSettleStatus()
    {
        // 状态： https://inews.gtimg.com/newsapp_ls/0/14888831794/0.jiketuchuang.png

        $itemBizStatus = $this->params['itemBizStatus'] ?? '';

        switch ($itemBizStatus) {
            case self::ITEMBIZ_STATUS_PAY_SUCCESS:
            case self::ITEMBIZ_STATUS_WRITEOFF_SUCCESS:
                return CouponOrder::C_AGENT_SETTLE_STATUS_NO; //还没到结算
                break;
            case self::ITEMBIZ_STATUS_SETTLE_SUCCESS:
                return CouponOrder::C_AGENT_SETTLE_STATUS_WAIT;//等待结算
                break;
            case self::ITEMBIZ_STATUS_INVALID:
            case self::ITEMBIZ_STATUS_UNKNOWN:
                return CouponOrder::C_AGENT_SETTLE_STATUS_NO; //无需结算
                break;
        }
        return CouponOrder::C_AGENT_SETTLE_STATUS_NO;
    }

    public function getStatus()
    {
        //状态 ： https://inews.gtimg.com/newsapp_ls/0/14888931121/0.jiketuchuang.png

        $itemStatus = $this->params['itemStatus'] ?? -1;
        //有可能这个itemStatus 为1 支付状态；但是 是无效订单，所以需要判断是否是无效订单，给出状态

        $itemBizStatus = $this->params['itemBizStatus'] ?? '';

        if (in_array($itemBizStatus,[self::ITEMBIZ_STATUS_INVALID,self::ITEMBIZ_STATUS_UNKNOWN])){
            return CouponOrder::C_STATUS_RECHARGE_FAIL;
        }


        switch ($itemStatus) {
            case 0:
                return CouponOrder::C_STATUS_PROCESSING;
            case 1:
            case 2:
                return CouponOrder::C_STATUS_RECHARGE_SUCCESS;
                break;
            case 3:
            case 5:
                return CouponOrder::C_STATUS_RECHARGE_FAIL;
                break;
            default:
                \think\facade\Log::warning('未知美团联盟状态');
                throw new Exception('未知美团联盟结算状态');
                break;
        }
    }

    public function getExtData()
    {
        return $this->params;//把数据都存咯
    }
}